package com.key.web_admin.controller;


import com.key.common.base.RespResult;
import com.key.common.base.TranException;
import com.key.common.constants.Constants;
import com.key.common.utils.CosUtils;
import com.key.modules.dto.ICConditionDTO;
import com.key.modules.dto.ICustomersDTO;
import com.key.modules.service.ICustomersService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/iCustomers")
public class ICustomersController {

    @Autowired
    private ICustomersService iCustomersService;

    @PostMapping("/addICustomers")
    public RespResult addICustomers(@RequestBody ICustomersDTO iCustomersDTO){

        return iCustomersService.addICustomers(iCustomersDTO);
    }

    @GetMapping("/findAllICustomers")
    public RespResult findAllICustomers(@RequestBody ICConditionDTO icConditionDTO){

        return iCustomersService.findAllICustomers(icConditionDTO);
    }

    @GetMapping("/findICustomersById")
    public RespResult findICustomersById(@RequestParam("id") Integer id){

        return iCustomersService.findICustomersById(id);
    }

    @PostMapping("/updateICustomers")
    public RespResult updateICustomers(@RequestBody ICustomersDTO iCustomersDTO){

        return iCustomersService.updateICustomers(iCustomersDTO);
    }

    @PostMapping("/deleteICustomers")
    public RespResult deleteICustomers(@RequestBody List<Integer> ids){

        return iCustomersService.deleteICustomers(ids);
    }

    @PostMapping("/updateIAState")
    public RespResult updateIAState(@RequestBody List<Integer> ids){
        return iCustomersService.updateIAState(ids);
    }

    @PostMapping("/disDSta")
    public RespResult disDSta(@RequestBody List<Integer> ids){
        return iCustomersService.disDSta(ids);
    }

    @PostMapping("/enDSta")
    public RespResult enDSta(@RequestBody List<Integer> ids){
        return iCustomersService.enDSta(ids);
    }

    @PostMapping("/upload")
    public RespResult upload(@RequestParam("imgUrl") MultipartFile imgUrl) throws IOException {
        try {
            CosUtils.initCosClient();
            URL url = CosUtils.uploadFile(imgUrl, Constants.BUCKETNAME, CosUtils.formatKey(imgUrl));
            if(!url.toString().equals("")){
                return RespResult.success(url.toString());
            }else {
                throw new TranException("上传失败");
            }
        } finally{
            CosUtils.shutdownClient();
        }
    }
}
